package com.esiag.isidis.fi.pdsTeam.jms.consumer;

import java.io.IOException;
import java.io.Serializable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import com.esiag.isidis.fi.pdsTeam.jms.connection.AbstractConnectionFactory;

public abstract class AbstractConsumerFactory<T extends Serializable> {
  AbstractConnectionFactory connectionFactory;
  Session session;
  Class msgClass;

  public AbstractConsumerFactory(Class msgClass) {
    try {
      connectionFactory = getConnection(msgClass);
      connectionFactory.start();
      MessageConsumer consumer = connectionFactory.getConsumer(getQueue(msgClass));
      consumer.setMessageListener(new ScalabilityConsumer(this));
      new Thread(new Runnable() {

        public void run() {
          try {
            System.in.read();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }).start();
    } catch (JMSException e) {
      e.printStackTrace();
    }
  }

  public abstract void onMessage(Message message);

  public abstract AbstractConnectionFactory getConnection(Class msgClass) throws JMSException;;

  public abstract String getQueue(Class msgClass);

}
